﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;
using IDAL;
using System.Data.SqlClient;
using System.Configuration;


namespace DAL
{
    public  class PurchaseDal:IPurchaseGeneral
    {
        public static string str = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; 
        /// <summary>
        /// 返回商品一览的列表
        /// </summary>
        /// <returns></returns>
        public List<PurchaseModel> iPurchaseGeneralList()
        {
            using (SqlConnection conn = new SqlConnection(str))
            {
                conn.Open();
                string sql = "select * from purchasetable";
                SqlCommand cmd = new SqlCommand(sql, conn);

                SqlDataReader reader = cmd.ExecuteReader();

                List<PurchaseModel> list=new List<PurchaseModel> ();

                if(reader.HasRows)
                {
                    while(reader.Read())
                    {
                        PurchaseModel pur = new PurchaseModel();
                        pur.PurchaseID = Convert.ToInt32(reader["PurchaseID"]);
                        pur.PurchasePrice = Convert.ToInt32(reader["PurchasePrice"]);
                        pur.commodityID = reader["commodityID"].ToString();
                        pur.FlowersName = reader["FlowersName"].ToString();
                        list.Add(pur);
                    }
                }
                return list;
                
            }
        }





        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="list2"></param>
        /// <returns></returns>
        public int iPurchaseOrderAddAll(List<Purchase_orderModel> list2)
        {
            using(SqlConnection conn =new SqlConnection (str))
            {
                conn.Open();
                StringBuilder builder = new StringBuilder();

                if(list2.Count>0)
                {
                    for(int i=0;i<list2.Count;i++)
                    {
                        builder.Append("insert into Purchase_orderTable values('" + list2[i].Invoice_number + "','" + list2[i].Supplier + "','" + list2[i].commodityID + "','" + list2[i].FlowersName + "'," + list2[i].PurchaseNumber + "," + list2[i].PurchasePrice + ")");

                    }
                }
                SqlCommand cmd = new SqlCommand(builder.ToString(), conn);
                return cmd.ExecuteNonQuery();

            }
        }
    }
}
